package vip.denglei.company.service.app.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import vip.denglei.company.mapper.AppAdoptionActivitiesMapper;
import vip.denglei.company.model.app.AppAdoptionActivities;
import vip.denglei.company.model.app.vo.AppAdoptionActivitiesByUserIdVO;
import vip.denglei.company.model.app.vo.AppUserAdoptionRecordsNumVO;
import vip.denglei.company.model.app.vo.AppUserAdoptionRecordsVO;
import vip.denglei.company.service.app.AppAdoptionActivitiesService;
import vip.denglei.company.service.app.AppUserAdoptionRecordsService;
import vip.denglei.company.service.app.AppUserReceiveRecordsService;
import vip.denglei.company.util.HttpResult;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class AppAdoptionActivitiesServiceImpl extends ServiceImpl<AppAdoptionActivitiesMapper, AppAdoptionActivities> implements AppAdoptionActivitiesService {
    @Resource
    AppAdoptionActivitiesMapper appAdoptionActivitiesMapper;
    @Resource
    private AppUserAdoptionRecordsService appUserAdoptionRecordsService;
    @Resource
    private AppUserReceiveRecordsService appUserReceiveRecordsService;

    @Override
    public HttpResult queryAppAdoptionActivitiesByUserId(Integer adoptionActivitiesId, String userId) {
        AppAdoptionActivitiesByUserIdVO appAdoptionActivitiesByUserIdVO = new AppAdoptionActivitiesByUserIdVO();
        AppAdoptionActivities appAdoptionActivities = this.getById(adoptionActivitiesId);
        if (Objects.nonNull(appAdoptionActivities)) {
            appAdoptionActivitiesByUserIdVO.setId(appAdoptionActivities.getId());
            appAdoptionActivitiesByUserIdVO.setAdoptionActivityName(appAdoptionActivities.getAdoptionActivityName());
            appAdoptionActivitiesByUserIdVO.setAdoptionActivityPictures(appAdoptionActivities.getAdoptionActivityPictures());
            //活动配置
            AppUserAdoptionRecordsNumVO appUserAdoptionRecordsNumVO1 = new AppUserAdoptionRecordsNumVO();
            appUserAdoptionRecordsNumVO1.setAppAdoptionActivitiesId(appAdoptionActivitiesByUserIdVO.getId());
            appUserAdoptionRecordsNumVO1.setActivitiesNumType(1);
            appUserAdoptionRecordsNumVO1.setActivitiesNum(15);
            appUserAdoptionRecordsNumVO1.setAdoptionsPrice(BigDecimal.valueOf(12000));
            AppUserAdoptionRecordsNumVO appUserAdoptionRecordsNumVO2 = new AppUserAdoptionRecordsNumVO();
            appUserAdoptionRecordsNumVO2.setAppAdoptionActivitiesId(appAdoptionActivitiesByUserIdVO.getId());
            appUserAdoptionRecordsNumVO2.setActivitiesNumType(2);
            appUserAdoptionRecordsNumVO2.setActivitiesNum(25);
            appUserAdoptionRecordsNumVO2.setAdoptionsPrice(BigDecimal.valueOf(10800));
            appAdoptionActivitiesByUserIdVO.setAppUserAdoptionRecordsNumVOList(Lists.newArrayList(appUserAdoptionRecordsNumVO1, appUserAdoptionRecordsNumVO2));
            //获取活动认领数量
            List<AppUserAdoptionRecordsVO> appUserAdoptionRecordsVOList = appAdoptionActivitiesMapper.getAppUserAdoptionRecordsListByUserId(userId, adoptionActivitiesId);
            appAdoptionActivitiesByUserIdVO.getAppUserAdoptionRecordsNumVOList().stream().forEach(appUserAdoptionRecordsNumVO -> {
                Optional<AppUserAdoptionRecordsVO> appUserAdoptionRecordsVO = appUserAdoptionRecordsVOList.stream().filter(appUserAdoptionRecordsVO1 -> appUserAdoptionRecordsVO1.getActivitiesNumType().equals(appUserAdoptionRecordsNumVO.getActivitiesNumType())).findFirst();
                if (appUserAdoptionRecordsVO.isPresent()) {
                    appUserAdoptionRecordsNumVO.setAdoptionsNum(appUserAdoptionRecordsVO.get().getAdoptionsNum());
                    appUserAdoptionRecordsNumVO.setReceiveNum(appUserAdoptionRecordsVO.get().getReceiveNum());
                } else {
                    appUserAdoptionRecordsNumVO.setAdoptionsNum(0);
                    appUserAdoptionRecordsNumVO.setReceiveNum(0);
                }

            });

        }
        return HttpResult.success(appAdoptionActivitiesByUserIdVO);
    }
}
